package com.example.demo.controller;

import com.example.demo.client.SearchApiService;
import com.example.demo.pojo.SearchQuestion;
import com.example.service.ESQuestionService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * Author by admin, Email xx@xx.com, Date on 2020/5/29.
 * PS: Not easy to write code, please indicate.
 */
@RestController
@CrossOrigin
public class ESQuestionController {
    @Autowired
    ESQuestionService esQuestionService;
    @Autowired
    SearchApiService searchApiService;

    @GetMapping("save")
    public Result savequestion(SearchQuestion question){
        esQuestionService.save(question);
        return new Result(true, StatusCode.OK,"保存成功");
    }
    @GetMapping("search/{key}/{page}/{size}")
    public Result findBykey(@PathVariable String key,@PathVariable int page,@PathVariable int size){
        Page<SearchQuestion> questionPage =esQuestionService.findBykey(key,page,size);
        return new Result(true,StatusCode.OK,"查询成功",
                new PageResult<SearchQuestion>(questionPage.getTotalElements(),questionPage.getContent()));
    }
    @GetMapping("testapi")
    public Result testapi(){
        Result result = searchApiService.queryAllQuestion();
        return new Result(true,StatusCode.OK,"查询成功",result);
    }

}
